package org.robert.export_client.excel.builder;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.entity.CellStyleEntity;
import org.robert.export_client.excel.entity.WorkbookPathWrite;
import org.robert.export_client.excel.metadata.AbstractSheetBuilder;
import org.robert.export_client.excel.metadata.AbstractWorkbookBuilder;
import org.robert.export_client.excel.metadata.SourceSheetBuilder;
import org.robert.export_client.excel.util.ExcelUtil;

import java.io.IOException;
import java.util.Map;

/**
 * ExcelWorkbookBuilder:
 *
 * @author zhaoshengtao
 * @date 2024/01/19
 */
public class WorkbookDataPathBuilder extends AbstractWorkbookBuilder {

    public WorkbookDataPathBuilder(HSSFWorkbook workbook, String path, String fileName) {
        super(workbook, path, fileName);
    }

    public static WorkbookDataPathBuilder create(String path) throws IOException {
        return create(path, null);
    }

    public static WorkbookDataPathBuilder create(String path, String fileName) throws IOException {
        HSSFWorkbook workbook = ExcelUtil.createWorkBook();
        return new WorkbookDataPathBuilder(workbook, path, fileName);
    }

    @Override
    public ExcelSheetDataPathBuilder createSheet(String sheetName, Class<?> clazz,
                                            Map<String, Map<String, String>> dictMap) throws Exception {
        if(StringUtils.isBlank(sheetName)){
            sheetName = "sheet1";
        }
        HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
        return new ExcelSheetDataPathBuilder(this, sheet, clazz, dictMap);
    }

    @Override
    public AbstractSheetBuilder createSheet(String sheetName, Class<?> clazz, Map<String, Map<String, String>> dictMap, CellStyleEntity titleStyle) throws Exception {
        if (StringUtils.isBlank(sheetName)) {
            sheetName = "sheet1";
        }
        HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
        return new ExcelSheetDataPathBuilder(this, sheet, clazz, dictMap, titleStyle);
    }
    @Override
    public ExcelSheetSourcePathBuilder createSheet(String sheetName, Class<?> clazz,
                                                 Map<String, Map<String, String>> dictMap, Integer limit) throws Exception {
        if(StringUtils.isBlank(sheetName)){
            sheetName = "sheet1";
        }
        HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
        return new ExcelSheetSourcePathBuilder(this, sheet, clazz, dictMap, limit);
    }

    @Override
    public SourceSheetBuilder createSheet(String sheetName, Class<?> clazz, Map<String, Map<String, String>> dictMap, Integer limit, CellStyleEntity titleStyle) throws Exception {
            if(StringUtils.isBlank(sheetName)){
                sheetName = "sheet1";
            }
            HSSFSheet sheet = ExcelUtil.createSheet(this.getWorkbook(), sheetName);
            return new ExcelSheetSourcePathBuilder(this, sheet, clazz, dictMap, limit, titleStyle);
    }

    @Override
    public WorkbookPathWrite build(){
        return new WorkbookPathWrite(this);
    }

}
